package com.room.controller.teacher;

import com.room.pojo.DormResult;
import com.room.pojo.Repairitems;
import com.room.pojo.Student;
import com.room.pojo.Teachaer;
import com.room.service.TeacherService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建人 lin
 * @创建时间 2019/10/14
 * @描述
 */
@RestController
@RequestMapping("/teacher/manager")
public class TeacherController {

    @Autowired
    TeacherService teacherService;


    /**
     * 修改教师信息
     * @param teachaer
     * @return
     */
    @RequestMapping(value = "/tea",method = RequestMethod.PUT)
    public DormResult updateStudent(Teachaer teachaer){
        if(teacherService.updateTeacher(teachaer)==1){
            return DormResult.ok("修改成功");
        }
        else{
            return DormResult.error("修改失败");
        }
    }

    /**
     * 删除指定id的教师
     * @param ids
     * @return
     */
    @RequestMapping(value = "/tea/{ids}",method = RequestMethod.DELETE)
   public DormResult delteByTeaId(@PathVariable String ids){
        if(teacherService.deleteTeaById(ids)){
            return DormResult.ok("删除教师成功");
        }
        return DormResult.error("删除教师失败");
    }

    /**
     * 获得学生的分页数据
     * @param page
     * @param size
     * @return返回学生的分页数据
     */
    @RequestMapping(value = "/tea",method = RequestMethod.GET)
    public Map<String,Object> getTeacherByPage(
            @RequestParam(defaultValue = "1")Integer page,
            @RequestParam(defaultValue = "6")Integer size
    ){

        Map<String,Object> map = new HashMap<String,Object>();
        List<Teachaer> teachers = teacherService.getTeaByPage(page,size);
        long total = teacherService.getTotal();
        map.put("teachers",teachers);
        map.put("total",total);
        return map;
    }




    @RequestMapping(value = "/tea",method = RequestMethod.POST)
    public DormResult addTeacher(Teachaer teachaer){
        String teacherNo = teacherService.getMaxId();
        int maxID = Integer.parseInt(teacherNo);
        String teaNo = String.valueOf(maxID+1);
        teachaer.setTeacherNo(teaNo);
        if(teacherService.insertTea(teachaer)==1){
            return DormResult.ok("添加成功");
        }
        return DormResult.error("添加失败");
    }


    @RequestMapping(value = "/getTeacherByNo",method = RequestMethod.GET)
    public Map<String,Object> getTeacherByNo(@RequestParam String teacherNo){
        Map<String,Object> resultMap = new HashMap<String,Object>();
        Teachaer teachaer = teacherService.getTeacherByNo(teacherNo);
        resultMap.put("teacher",teachaer);
        return resultMap;
    }

}
